
package com.weiqiao.mall.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.mongodb.client.model.Filters;
import com.weiqiao.mall.domain.GiftCardEntity;
import com.weiqiao.mall.repository.IGiftRepository;
import com.weiqiao.mall.service.IGiftService;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description:
 * @Author: 陈忠强
 * @date: 2018/6/14.
 */
@Service("GiftBizImpl")
public class GiftServiceImpl extends BaseWeiXinServiceImpl implements IGiftService {


    @Autowired
    @Qualifier("GiftRepositoryImpl")
    private IGiftRepository iGiftDao;


    @Override
    public GiftCardEntity findGiftCardById(String card_id) {
        String s = iGiftDao.findGiftCardById(Filters.eq("card_id", card_id));
        if (StringUtil.isBlank(s)) {
            return null;
        } else {
            return new Gson().fromJson(s, GiftCardEntity.class);
        }

    }

    @Override
    public boolean deleteById(int company_id, String card_id) {
        Bson filter1 = Filters.eq("company_id", company_id);
        Bson filter2 = Filters.eq("card_id", card_id);
        Bson filter = Filters.and(filter1, filter2);
        Document doc = new Document();
        doc.append("is_delete", 1);
        return iGiftDao.deleteById(filter, doc, false);
    }

    @Override
    public List<GiftCardEntity> getAllGiftCardList(Bson filter) {
        List<String> jsonList = iGiftDao.getGiftCardList(filter);
        List<GiftCardEntity> list = new ArrayList<>();
        if (jsonList != null && jsonList.size() > 0) {
            for (String json :
                    jsonList) {
                list.add(new Gson().fromJson(json, GiftCardEntity.class));
            }
        }
        return list;
    }
}
